from datetime import datetime, timedelta, time

from odoo import fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools.misc import format_datetime


class ReportFinishedProductwizard(models.TransientModel):
    _name = 's.report.finished.product.wizard'
    _description = 'finished Product Report wizard'
    _order = 'product_id'

    start_date = fields.Date('Start Date')
    end_date = fields.Date('End Date')
    product_id = fields.Many2one('product.product', 'Product')
    production_type = fields.Selection([
        ('all', 'all'),
        ('batch', 'batch'),
        ('sample', 'sample'),
        ('feed', 'feed')
    ], string='Production Type', default='all')

    def action_download(self):
        if self.start_date and self.end_date:
            start_date = min(self.start_date, self.end_date)
            end_date = max(self.start_date, self.end_date)
        else:
            start_date = self.start_date
            end_date = self.end_date
        if end_date:
            end_date += timedelta(days=1)

        return {
            'name': _('Finished Product Report'),
            'view_mode': 'tree',
            'res_model': 's.finished.product.report',
            'type': 'ir.actions.act_window',
            'context': {
                'start_date': start_date and start_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                'end_date': end_date and end_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                'product_id': self.product_id.id,
                'production_type': self.production_type
            },
        }
